package cn.kbyue.asyncdemo.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/async")
public class AsyncController {

    @Resource(name="threadPoolTaskExecutor")
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @GetMapping("loop")
    public ResponseEntity<String> getResult() {
        loop();
        return ResponseEntity.ok("success...");
    }

    public void loop() {
        threadPoolTaskExecutor.execute(() -> {
            //业务代码
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    System.out.println("error ====>" + e.getMessage());
                }
                System.out.println(Thread.currentThread().getName() + "===" + LocalDateTime.now());
            }
        });
    }

}
